/*
 *	CompanyInfo.cpp
 *
 * 	Copyright (C) 2011 Aaron Huang
 * 	This file is part of StackPainter.
 *
 * 	StackPainter is free software: you can redistribute it and/or modify
 * 	it under the terms of the GNU General Public License as published by
 * 	the Free Software Foundation, either version 3 of the License, or
 * 	(at your option) any later version.
 *
 * 	StackPainter is distributed in the hope that it will be useful,
 * 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 	GNU General Public License for more details.
 *
 * 	You should have received a copy of the GNU General Public License
 * 	along with StackPainter.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "CompanyInfo.h"

const QString DUPLICATE_EPS = "Add duplicate EPS to CompanyInfo ";

CompanyInfo::CompanyInfo(int num, QString name, int year, double EPS) {
	_num = num;
	_name = name;
	_EPSs[year] = EPS;
}

CompanyInfo::~CompanyInfo() {}


int CompanyInfo::getNum() const {
	return _num;
}

QString CompanyInfo::getName() const {
	return _name;
}

const QMap<int, double>* CompanyInfo::getEPSs() const {
	return &_EPSs;
}

void CompanyInfo::CompanyInfo::addEPS(int year, double EPS) throw(QString) {
	if (_EPSs.find(year) != _EPSs.end()) {
		throw(DUPLICATE_EPS + QString::number(_num));
	}
	_EPSs[year] = EPS;
}

bool CompanyInfo::isListedOver(int year) const {
	return (_EPSs.size() > year);
}

bool CompanyInfo::isEPSAscending() const {
	if (!isListedOver(2)) {
		return false;
	}

	QMap<int, double>::const_iterator EPSIt = _EPSs.begin();
	double currentEPS = EPSIt.value();
	++EPSIt;
	while (EPSIt != _EPSs.end()) {
		if (EPSIt.value() < currentEPS) {
			return false;
		}
		currentEPS = EPSIt.value();
		++EPSIt;
	}
	return true;
}

string CompanyInfo::toStdString() const {
	string EPSs = "";
	QMap<int, double>::const_iterator EPSIt = _EPSs.begin();
	while (EPSIt != _EPSs.end()) {
		EPSs = EPSs + " year : " + QString::number(EPSIt.key()).toStdString()
				+ " , EPS : " + QString::number(EPSIt.value()).toStdString()
				+ " ";
		++EPSIt;
	}
	return "Num : " + QString::number(_num).toStdString()
			+ " , Name : " + _name.toStdString()
			+ EPSs + "\n";
}
